import 'normalize.css/normalize.css' // a modern alternative to CSS resets
import '@/styles/index.scss' // global css
import Vue from 'vue'
import Element from 'element-ui'
import VueI18n from 'vue-i18n'
import jwchat from 'jwchat'
import Contextmenu from 'vue-contextmenujs'
import dataV from '@jiaminghi/data-view'
import VueClipboard from 'vue-clipboard2'

import en from '@/assets/lang/en'
import zh from '@/assets/lang/zh'
import pt from '@/assets/lang/pt'

import App from './App'
import store from './store'
import router from './router'
import './permission' // permission control
import './util/error-log' // error log
import {
  getCompanyId,
  getHostName,
  getLanguage, getShortCompanyName,
  getSize,
  setCompanyIconUrl,
  setCompanyId,
  setCompanyName,
  setEnableDemoAccount,
  setEnablePhoneRegister,
  setEnableRegister,
  setEnableServerLinkage,
  setForReferenceInfo,
  setHomePageUrl,
  setLanguage,
  setLat,
  setLng,
  setNetModel,
  setOfficialUrl,
  setQrCodeBaseUrl,
  setShortCompanyName,
  setWebSocketUrl
} from '@/util/storage'
import { getCompanyWithoutToken } from '@/api/iot/iotCompanyApi'
import { getSystemInfo } from '@/api/iot/iotSystemApi'
import { getCompanyConfigDevice } from '@/api/iot/iotCompanyConfigDeviceApi'
import { listAllSystemDictionaries } from '@/api/common/commonSystemDictionariesApi'
import { listCompanyDictionaries } from '@/api/common/commonCompanyDictionariesApi'
import { initMap } from '@/util/mapTool'

window.onerror((e) => {
  console.error(e)
})

getSystemInfo({}).then(res => {
  const { code, data } = res
  if (code === '200') {
    setNetModel(data.netModel)
    setWebSocketUrl(data.wsUrl)
    initMap()
  }
})

listAllSystemDictionaries({}).then(res => {
  const {
    code,
    data
  } = res
  if (code === '200') {
    data.forEach(item => {
      switch (item.language) {
        case 'zh':
          zh[item.keyName] = item.value
          break
        case 'en':
          en[item.keyName] = item.value + ' '
          break
        case 'pt':
          pt[item.keyName] = item.value + ' '
          break
      }
    })
  }
})

const changeFavicon = link => {
  let $favicon = document.querySelector('link[rel="icon"]')
  // If a <link rel="icon"> element already exists,
  // change its href to the given link.
  if ($favicon !== null) {
    $favicon.href = link
    // Otherwise, create a new element and append it to <head>.
  } else {
    $favicon = document.createElement('link')
    $favicon.rel = 'icon'
    $favicon.href = link
    document.head.appendChild($favicon)
  }
}

getCompanyWithoutToken({
  domainName: getHostName()
}).then(res => {
  const { code, data } = res
  if (code === '200') {
    if (!data.status) {
      console.error('公司禁用')
      return
    }
    setEnableDemoAccount(data.enableDemoAccount)
    if (!getLanguage()) {
      setLanguage(data.language)
    }
    setCompanyName(data.companyName)
    setShortCompanyName(data.shortCompanyName)
    document.title = data.shortCompanyName
    setCompanyId(data.companyId)
    setOfficialUrl(data.officialUrl)
    setEnablePhoneRegister(data.enablePhoneRegister)
    setEnableRegister(data.enableRegister)
    setCompanyIconUrl(data.iconUrl)
    changeFavicon(data.iconUrl)
    setHomePageUrl(data.homePageUrl)
    setForReferenceInfo(data.forReferenceInfo)
    setQrCodeBaseUrl(data.qrCodeBaseUrl)
    setLat(data.lat)
    setLng(data.lng)
    init()
    getCompanyConfigDevice({
      companyId: data.companyId
    }).then(res2 => {
      if (res2.code === '200') {
        setEnableServerLinkage(res2.data.enableServerLinkage)
      }
    })
  }
})

let app = null

function init() {
  listCompanyDictionaries({
    companyId: getCompanyId()
  }).then(res => {
    const {
      code,
      data
    } = res
    if (code === '200') {
      data.forEach(item => {
        switch (item.language) {
          case 'zh':
            zh[item.keyName] = item.value
            break
          case 'en':
            en[item.keyName] = item.value + ' '
            break
          case 'pt':
            pt[item.keyName] = item.value + ' '
            break
        }
      })
    }
  })

  Vue.use(dataV)
  Vue.use(VueI18n)
  Vue.use(Contextmenu)
  Vue.use(jwchat)
  // Create VueI18n instance with options
  const messages = {
    en: en,
    zh: zh,
    pt: pt
  }
  const i18n = new VueI18n({
    locale: getLanguage() || 'zh', // set locale
    messages // set locale messages
  })
  Vue.use(Element, {
    size: getSize() || 'mini', // set element-ui default size
    i18n: (key, value) => i18n.t(key, value)
  })

  Vue.use(VueClipboard)
  Vue.config.productionTip = false

  app = new Vue({
    i18n,
    el: '#app',
    router,
    store,
    data() {
      return {}
    },
    render: h => h(App)
  })
}

export function getContext() {
  return app
}
